package com.travel.goods.controller;

import com.travel.common.base.BaseApiService;
import com.travel.common.base.Resp;
import com.travel.common.exception.LogicException;
import com.travel.common.utils.OperationFileUtil;
import com.travel.goods.entity.Goods;
import com.travel.goods.entity.GoodsUserRel;
import com.travel.goods.service.GoodsService;
import com.travel.goods.service.GoodsUserRelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.List;

/**
 * @classDesc: 功能描述:()
 * @author:<a href="leeue@foxmail.com">李月</a>
 * @Version:v1.0
 * @createTime:2019年3月19日 下午4:42:10
 */
@Api(tags = "商品模块")
@RestController
@Slf4j
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    BaseApiService baseApiService;

    @Autowired
    GoodsService goodsService;

    @Resource
    GoodsUserRelService goodsUserRelService;

    @Value("${goodImgUrl}")
    String imgPath;

    @Autowired
    HttpServletRequest request; //这里可以获取到request


    /**
     * 根据城市来查找商品
     *
     * @param city
     * @return
     */
    @ApiOperation(value = "根据城市来查找商品")
    @ApiImplicitParams({@ApiImplicitParam(name = "city", value = "商品名称", dataType = "String", required = true)})
    @PostMapping("/findByGoodsCity")
    public Resp findByGoodsCity(String city) {

        List<Goods> goods = goodsService.findByGoodsCity(city);
        Collections.sort(goods);
        return baseApiService.setResultSuccess(goods);
    }

    /**
     * 添加商品 接口
     *
     * @param goods
     * @return
     */
    @ApiOperation(value = "添加商品接口")
    @ApiImplicitParams({@ApiImplicitParam(name = "goods", value = "商品名称", dataType = "Goods", required = true)})
    @PostMapping("/addGoods")
    public Resp addGoods(Goods goods) {
        if (goods.getUserId() == null) {
            return baseApiService.setResultError("商户没有登录,不可以发布商品");
        }
        if (StringUtils.isEmpty(goods.getGoodsName())) {
            return baseApiService.setResultError("商品名称不能为空");
        }

        try {
            goods = goodsService.addGoods(goods);
            // 上传图片 保存起来
            OperationFileUtil.uploadFiles(request, imgPath, goods.getId());

        } catch (Exception e) {
            e.printStackTrace();
            log.error("###错误信息:" + e.getMessage());
            return baseApiService.setResultError("商品发布失败，上传商品图片错误");
        }
        return baseApiService.setResultSuccess(goods);
    }

    /**
     * 根据商品名称模糊查找
     *
     * @param goodsName
     * @return
     */

    @ApiOperation(value = "根据用户ID和订单ID来查询订单状态")
    @ApiImplicitParams({@ApiImplicitParam(name = "goodsName", value = "商品名称", dataType = "String", required = true)})
    @PostMapping("/findByGoodsName")
    public Resp<List<Goods>> findByGoodsName(String goodsName) {



        return  Resp.ok(goodsService.findByGoodsName(goodsName));
    }


    /**
     * 根据商品名称模糊查找
     *
     * @param goodsUserRel
     * @return
     */

    @ApiOperation(value = "收藏商品")
    @ApiImplicitParams({@ApiImplicitParam(name = "goodsUserRel",
            value = "商品名称", dataType = "GoodsUserRel", required = true)})
    @PostMapping("/collectGoods")
    public Resp collectGoods(@RequestBody GoodsUserRel goodsUserRel) {


        return baseApiService.setResultSuccess(goodsUserRelService.save(goodsUserRel));
    }

}
